有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从文件中查找命令行中指定的单词

我试图从文件中搜索命令行中指定的单词。但是,在运行时,它不显示任何内容。有什么问题吗?这是我代码的一部分。谢谢你帮助我!

class ClientHandler extends Thread {
        private Socket client;
        private Scanner input;
        private PrintWriter output;
        private ArrayList<String> quotes;
        public ClientHandler3(Socket socket, String file) {
            client = socket;
            try {
                BufferedReader buffer = new BufferedReader(new FileReader(file));
                BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream())); 
                String line = reader.readLine();
                try {
                    int ctr = 0;
                    quotes = new ArrayList<String>();
                    while(line != null){
                        quotes.add(ctr, line);

                        ctr++;
                        line = buffer.readLine();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }


            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            try {
                input = new Scanner(client.getInputStream());
                output = new PrintWriter(client.getOutputStream(), true);
            }
            catch(IOException e) {
                e.printStackTrace();
            }
        }


        public void run(String[] args) {
            String target;
            String message = "";
                target= args[3];
                for(int i = 0; i<quotes.size(); i++){
                    if(quotes.get(i).toUpperCase().contains(target.toUpperCase())){
                        output.println(quotes.get(i));
                    }
                }
                output.println("|");
            try {
                if (client != null) {
                    System.out.println("Closing down connection...");
                    client.close();
                }
            }
            catch(IOException e) {
                System.out.println("Unable to disconnect!");
            }
        }
    }

共 (1) 个答案

  1. # 1 楼答案

    您的客户端读取要搜索的单词args[1](主机)、args[2](端口),但不读取args[3]

    您的代码也很难阅读,因为它不遵守标准命名约定。类以大写字母开头,而方法以小写字母开头。你的代码做的正好相反